package dhbw.GUI.Panels;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import dhbw.Data.Language;
import dhbw.Data.Settings;
import Main.MyController;

public class PanelOptions extends JPanel implements ActionListener {

	private MyController myc;
	private Color bgcolor;
	private JFrame parent;
	private int lang;
	private JRadioButton radioRed, radioGreen, radioBlue, radioYellow,
			radioWhite, radioGerman, radioEnglish;
	private JButton bt_save, bt_cancel;
	JPanel panelColorOptions, panelLanguageOptions;

	public PanelOptions(MyController myc, Color bgcolor, int lang, JFrame parent) {
		this.myc = myc;
		this.bgcolor = bgcolor;
		this.lang = lang;
		this.parent = parent;
		build();
		set();
	}

	private void build() {
		this.setBackground(Settings.BG_COLOR);
		CellConstraints cc = new CellConstraints();
		String cols = "16dlu,pref,12dlu,pref,16dlu";
		String rows = "6dlu,pref,12dlu,pref,12dlu,pref,6dlu";
		FormLayout grid = new FormLayout(cols, rows);
		this.setLayout(grid);
		bt_save = new JButton(Language.SAVE[myc.getLanguage()]);
		bt_cancel = new JButton(Language.CANCEL[myc.getLanguage()]);
		bt_save.addActionListener(this);
		bt_cancel.addActionListener(this);
		radioRed = new JRadioButton(Language.RED[myc.getLanguage()]);
		radioGreen = new JRadioButton(Language.GREEN[myc.getLanguage()]);
		radioBlue = new JRadioButton(Language.BLUE[myc.getLanguage()]);
		radioWhite = new JRadioButton(Language.WHITE[myc.getLanguage()]);
		radioYellow = new JRadioButton(Language.YELLOW[myc.getLanguage()]);
		radioEnglish = new JRadioButton(Language.ENGLISH[myc.getLanguage()]);
		radioGerman = new JRadioButton(Language.GERMAN[myc.getLanguage()]);

		ButtonGroup groupLanguage = new ButtonGroup();
		groupLanguage.add(radioGerman);
		groupLanguage.add(radioEnglish);

		ButtonGroup groupColor = new ButtonGroup();
		groupColor.add(radioBlue);
		groupColor.add(radioGreen);
		groupColor.add(radioRed);
		groupColor.add(radioYellow);
		groupColor.add(radioWhite);

		radioBlue.addActionListener(this);
		radioGreen.addActionListener(this);
		radioRed.addActionListener(this);
		radioWhite.addActionListener(this);
		radioYellow.addActionListener(this);

		radioGerman.addActionListener(this);
		radioEnglish.addActionListener(this);

		panelColorOptions = new JPanel();
		panelLanguageOptions = new JPanel();
		panelColorOptions.setBackground(Settings.BG_COLOR);
		panelLanguageOptions.setBackground(Settings.BG_COLOR);
		panelLanguageOptions.setLayout(new GridLayout(2, 1));
		panelColorOptions.setLayout(new GridLayout(5, 1));

		panelLanguageOptions.add(radioEnglish);
		panelLanguageOptions.add(radioGerman);

		panelColorOptions.add(radioBlue);
		panelColorOptions.add(radioGreen);
		panelColorOptions.add(radioRed);
		panelColorOptions.add(radioWhite);
		panelColorOptions.add(radioYellow);

		this.add(new JLabel(Language.LANGUAGE[myc.getLanguage()]), cc.xy(2, 2));
		this.add(new JLabel(Language.COLOR[myc.getLanguage()]), cc.xy(2, 4));
		this.add(panelLanguageOptions, cc.xy(4, 2));
		this.add(panelColorOptions, cc.xy(4, 4));
		this.add(bt_save, cc.xy(2, 6));
		this.add(bt_cancel, cc.xy(4, 6));
	}

	private void set() {
		// Set the settings
		if (this.lang == Language.LENGLISH)
			radioEnglish.setSelected(true);
		if (this.lang == Language.LGERMAN)
			radioGerman.setSelected(true);
		// Colors
		if (this.bgcolor.equals(Settings.YELLOW))
			radioYellow.setSelected(true);
		if (this.bgcolor.equals(Settings.RED))
			radioRed.setSelected(true);
		if (this.bgcolor.equals(Settings.BLUE))
			radioBlue.setSelected(true);
		if (this.bgcolor.equals(Settings.WHITE))
			radioWhite.setSelected(true);
		if (this.bgcolor.equals(Settings.GREEN))
			radioGreen.setSelected(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(bt_save)) {
			myc.getActiveUser().setBgColor(this.bgcolor);
			myc.getActiveUser().setLanguage(this.lang);
			myc.saveAll();
			parent.dispose();
			myc.logout();
		} else if (e.getSource().equals(bt_cancel)) {
			parent.dispose();
		} else {
			if (e.getSource().equals(radioGerman))
				this.lang = 0;
			if (e.getSource().equals(radioEnglish))
				this.lang = 1;
			if (e.getSource().equals(radioYellow))
				this.bgcolor = Settings.YELLOW;
			if (e.getSource().equals(radioRed))
				this.bgcolor = Settings.RED;
			if (e.getSource().equals(radioGreen))
				this.bgcolor = Settings.GREEN;
			if (e.getSource().equals(radioWhite))
				this.bgcolor = Settings.WHITE;
			if (e.getSource().equals(radioBlue))
				this.bgcolor = Settings.BLUE;
			this.setBackground(this.bgcolor);
			panelColorOptions.setBackground(this.bgcolor);
			panelLanguageOptions.setBackground(this.bgcolor);
		}
	}
}